package divide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Class for reading input parameters.
 *
 * @author Muzalevskiy Vitaliy
 */
public class Reader {

    /**
     * Read the value of dividend from console. If not integer value entered to 
     * console return error massage with proposition to try again.
     *
     * @return <code> int </code> type value of entered to console number.
     */
    public static int readDividend() {
        int result = 0;
        String value;
        BufferedReader reader;
        System.out.println("Enter dividend value:");
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            value = reader.readLine();
            result = Integer.valueOf(value);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (NumberFormatException e) {
            System.out.println("Only integer numbers must be entered!");
            result = readDividend();
        }
        return result;
    }

    /**
     * Read the value of divisor from console. If not integer value entered to 
     * console or value is 0 return error massage with proposition to try again.
     *
     * @return <code> int </code> type value of entered to console number.
     */
    public static int readDivisor() {
        int result = 0;
        String value;
        BufferedReader reader;
        System.out.println("Enter divisor value:");
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            value = reader.readLine();
            result = Integer.valueOf(value);
            if(result == 0){
                throw new ArithmeticException(); 
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (NumberFormatException e) {
            System.out.println("Only integer numbers must be entered!");
            result = readDivisor();
        } catch (ArithmeticException e){
            System.out.println("Can not divide to 0!");
            result = readDivisor();
        }
        return result;
    }
}
